package sg.techstudio.suntec.validator;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import sg.techstudio.suntec.object.UserChangePasswordObj;

@Component
public class UserChangePasswordObjValidator {
	private static final Log log = LogFactory.getLog(UserChangePasswordObjValidator.class);
	
	public void validateChangepassword(UserChangePasswordObj u, ValidationContext context) {
		log.debug("validateChangepassword() entry");
		MessageContext messages = context.getMessageContext();
		
		if(StringUtils.isBlank(u.getNewPassword()))
			messages.addMessage(new MessageBuilder().error().source("newPassword")
					.code("required.field").build());
		if(StringUtils.isBlank(u.getNewPasswordConfirm()))
			messages.addMessage(new MessageBuilder().error().source("newPasswordConfirm")
					.code("required.field").build());
		else if(!StringUtils.equalsIgnoreCase(u.getNewPassword(), u.getNewPasswordConfirm()))
			messages.addMessage(new MessageBuilder().error().source("newPasswordConfirm")
					.code("different.password").build());
	}
}
